import { NotifyError, NotifyOk } from '@/utils/utils'
import axios, {AxiosRequestConfig,AxiosResponse } from 'axios'
import NProgress from 'nprogress'
import { hideFullLoading, showFullLoading } from '@/hooks/loading'

export interface CustomAxiosRequestConfig extends AxiosRequestConfig {
    noLoading?: boolean;
}

const service = axios.create({
    baseURL:'/mock',
    timeout:3000
})

const errorMsg = (code:number) => {
    if(code == 200){
        // return NotifyOk()
    }else{
        return NotifyError()
    }
}

const startProgress = () => {
    NProgress.start()
  }

  const stopProgress = () => {
    NProgress && NProgress.isStarted() && NProgress.done()
  }

service.interceptors.request.use(

    (config: CustomAxiosRequestConfig) => {
        //noLoading为指定该请求是否展示全屏加载
        config.noLoading || showFullLoading()
        startProgress()
        if(config.headers){
            config.headers['X-Requested-With'] = 'XMLHttpRequest'
            config.headers['token'] = 'd9c9b6caa961e200ef63fff56b6d5a03'
        }
        return config
    },(err) => {

    }

)

service.interceptors.response.use(
    (res:AxiosResponse) => {
        hideFullLoading()
        stopProgress()
        errorMsg(res.data.code)
        return res.data
    },(err) => {
        stopProgress()
        hideFullLoading()
    }
)

export const GET = (url:string,params?:object,config?:object) => {
    return service.get(url,{params,...config})
}

export default service